package com.study.d016event;

import java.io.Serializable;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/** 自定义拖拽
*@description
*@author xtayhame
*date 2020年3月8日
*version
*since v1.0
*/
public class DragSelfMain extends Application{
	public DataFormat personFormat = new DataFormat("person/data");

	public static void main(String[] args) {
		launch(args);

	}

	@Override
	public void start(Stage stage) throws Exception {
		Person person = new Person("张三", 18, "file:E:\\javafxworkspace\\lianxidemo\\src\\image\\timg.gif");

		AnchorPane anchorPane = new AnchorPane();
		Button b1 = new Button(person.getName());
		VBox vBox1 = getVBox();

		anchorPane.getChildren().addAll(b1,vBox1);
		AnchorPane.setLeftAnchor(b1, 20.0);
		AnchorPane.setLeftAnchor(vBox1, 200.0);

		AnchorPane.setTopAnchor(b1, 100.0);
		AnchorPane.setTopAnchor(vBox1, 10.0);

		stage.setScene(new Scene(anchorPane, 500, 400));
		stage.show();


		//用于截图
		VBox vBoxPicture = getVBox();
		//设置值
		TextField nameField1=(TextField)vBoxPicture.getChildren().get(1);
		TextField ageField2=(TextField)vBoxPicture.getChildren().get(2);
		ImageView imageView3=(ImageView)vBoxPicture.getChildren().get(3);


		nameField1.setText(person.getName());
		ageField2.setText(String.valueOf(person.getAge()));
		imageView3.setImage(new Image(person.getPhoto()));




		//第一步：给b1按钮添加拖拽监测事件
		b1.setOnDragDetected(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("setOnDragDetected");
				//
				ClipboardContent content = new ClipboardContent();
				content.put(personFormat, person);
				Dragboard db= b1.startDragAndDrop(TransferMode.COPY);
				db.setContent(content);

				//设置拖拽的图标，这里截取生成后的图片
				//先显示出来后才能截图
				//用于截图


				anchorPane.getChildren().add(vBoxPicture);

				WritableImage wi = new WritableImage(100, 150);
				vBoxPicture.snapshot(new SnapshotParameters(), wi);
				db.setDragView(wi);
				//截完图后删除
				anchorPane.getChildren().remove(vBoxPicture);

			}
		});

		//第二步：给vbox1添加拖拽划过事件
		vBox1.setOnDragOver(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				System.out.println("setOnDragOver");
				event.acceptTransferModes(event.getTransferMode());
			}
		});

		//第三步：处理vbox1拖拽松开事件
		vBox1.setOnDragDropped(new EventHandler<DragEvent>() {

			@Override
			public void handle(DragEvent event) {
				System.out.println("setOnDragDropped");

				//TransferMode dataFormat=event.getTransferMode().valueOf("person/data");

				if(event.getTransferMode() == TransferMode.COPY){
					Object object =event.getDragboard().getContent(personFormat);
					Person p = (Person)object;
					//设置值
					TextField nameField=(TextField)vBox1.getChildren().get(1);
					TextField ageField=(TextField)vBox1.getChildren().get(2);
					ImageView imageView=(ImageView)vBox1.getChildren().get(3);

					System.out.println("图片路径：" + p.getPhoto());
					nameField.setText(p.getName());
					ageField.setText(String.valueOf(p.getAge()));
					imageView.setImage(new Image(p.getPhoto()));
				}

			}
		});

	}

	public VBox getVBox(){
		VBox vb=new VBox(10);
		vb.setStyle("-fx-background-color:#FFF456");
		Label title= new Label("个人详情");
		TextField  nameField = new TextField();
		TextField  ageField = new TextField();
		ImageView view = new ImageView();
		view.setFitHeight(200);
		view.setFitWidth(200);
		vb.getChildren().addAll(title,nameField,ageField,view);

		return vb;
	}

}

class Person implements Serializable{
	private static final long serialVersionUID = 1L;

	private String name;
	private int age;
	private String photo;

	public Person(String name,int age,String photo) {
		this.name = name;
		this.age =age;
		this.photo = photo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}



}
